/**************************************************************************************
 *  Copyright (c)  2010  Pablo Echeverri.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package data;

import data.tileables.Controllable;
import data.tileables.Fort;
import data.tileables.Structure;
import data.tileables.Unit;
/**************************************************************************************
 * Author(s): Pablo Echeverri
 * this class handles combat between controllables
 **************************************************************************************/
public class CombatManager
{
	private Map map;
	public CombatManager(Map a_map)
	{
		this.map=a_map;
	}
	/**************************************************************************************
	 * Author(s): Pablo Echeverri
	 * attack a unit/structure given the current x,y and the direction
	 **************************************************************************************/
	public void attack(int x, int y, int direction, Controllable controllable)
	{
		int[] pos= new int[2];
		pos=this.map.getAdjacentPosition(x, y, direction);
		Controllable controllableAdjacent=(this.map.getTopControllableAt(pos[0], pos[1], 0));
		System.out.println("Controllable Adjacent = " + controllableAdjacent);
		
		if(controllableAdjacent==null)
		{
			controllableAdjacent=(this.map.getTopControllableAt(pos[0], pos[1], 2));
		}
		if(controllableAdjacent==null)
		{
			controllableAdjacent=(this.map.getTopControllableAt(pos[0], pos[1], 3));
		}	
		if(controllableAdjacent!=null)
		{
			if(controllableAdjacent.getPlayerID()!=controllable.getPlayerID())//enemy controllable
			{
				controllableAdjacent.takeDamage(controllable.getStats()[3]);//give offensive damage
				System.out.println("Controllabe stats 3 " + controllable.getStats()[3]);
				controllable.takeDamage(controllableAdjacent.getStats()[4]);//take defensive damage
				System.out.println("ControllabeAdjacent stats 4 " + controllableAdjacent.getStats()[4]);
			}
			
		}
			
	}
	/**************************************************************************************
	 * Author(s): Pablo Echeverri
	 * fort attacks all enemy controllables given a radius of attack
	 **************************************************************************************/
	public void attackArea(int x, int y, Fort fort,int radius )
	{
		LinkedList<int[]> list = this.map.getArea(x, y, radius);
		list.setToFirst();
		for(int i=0; i<list.getSize();i++)
		{
			int[] pos=list.peekCurrent();
			Controllable controllableAdjacent=(this.map.getTopControllableAt(pos[0], pos[1], 0));
			
			if(controllableAdjacent==null)
			{
				controllableAdjacent=(this.map.getTopControllableAt(pos[0], pos[1], 2));
			}
			if(controllableAdjacent==null)
			{
				controllableAdjacent=(this.map.getTopControllableAt(pos[0], pos[1], 3));
			}	
			if(controllableAdjacent!=null)
			{
				if(controllableAdjacent.getPlayerID()!=fort.getPlayerID())//enemy controllable
				{
					controllableAdjacent.takeDamage(fort.getOffensiveDamage(pos[2]));//not implemented
					
					if(radius==1)
					{
						
						fort.takeDamage(controllableAdjacent.getStats()[4]);//take defensive damage
						
					}
					else
					{
						if(controllableAdjacent instanceof Fort)
						{
							int dmg=((Fort)controllableAdjacent).getDefensiveDamage(pos[2]);
							fort.takeDamage(dmg);
						}
					}
				}
			}
			list.next();
		}
	}
		
		
	
	
}